package weaver.interfaces.haozhi;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

public class ReduceKcslAction implements Action{

	@Override
	public String execute(RequestInfo request) {
		BaseBean baseBean = new BaseBean();
		
		RecordSet rs = new RecordSet();
		RecordSet rs1 = new RecordSet();
		
		Map<Integer,Double> map = new HashMap<Integer,Double>();
		List<String> list = new ArrayList<String>();
		//获取表单名称
		String mainTableName = request.getRequestManager().getBillTableName();
		//获取请求id
		String requestId = request.getRequestid();
		baseBean.writeLog("当前表单名字为=============》"+mainTableName);
		baseBean.writeLog("请求id为=============》"+requestId);
		String sql = "";
		String sql1 = "";
		sql = "SELECT * FROM "+mainTableName+"_dt1 WHERE mainid=(SELECT id FROM "+mainTableName+" WHERE requestid="+requestId+")";
		baseBean.writeLog(sql);
		rs.execute(sql);
		while(rs.next()){
			double sl = Util.getDoubleValue(rs.getString("sl"));
			int pxbm = Util.getIntValue(rs.getString("pxbmn"));//明细表品项编码
			map.put(pxbm, sl);
			sql1 = "SELECT * FROM uf_kcbd WHERE id="+pxbm;
			rs1.execute(sql1);
			baseBean.writeLog(sql1);
			double zksl = 0.00;
			String pxbmn = "";//库存品项编码
			if(rs1.next()){
				zksl = Util.getDoubleValue(rs1.getString("sl"));
				pxbmn = Util.null2String(rs1.getString("pxbm"));
				baseBean.writeLog("在库数量======="+zksl);
				baseBean.writeLog("品相编码=============="+pxbmn);
			}
			if(sl-zksl>0){
				list.add("【品相编码】："+pxbmn+"在库数量不足</br>");
			}
		}
		RecordSet rs2 = new RecordSet();
		String action = "1";
		RequestManager requestManager = request.getRequestManager();
		//如果list为空 则表示工单所有明细的数量库存充足
		baseBean.writeLog(list.size());
		if(list.size() == 0 || list == null){
			for(Entry<Integer, Double> entry : map.entrySet()){
				String sql2 = "";
				sql2 = "UPDATE uf_kcbd SET sl=sl-"+entry.getValue()+" WHERE id="+entry.getKey();
				baseBean.writeLog("执行库存扣减操作====================》"+sql2);
				rs2.execute(sql2);
			}
		}else{
			action = "0";
			requestManager.setMessagecontent(""+list);
			
		}
		return action;
	}

}
